package mail

import (
	"bytes"
	"github.com/jordan-wright/email"
	"html/template"
	"net/smtp"
	"regexp"
)

func SendMail(toMail, htmlTemple, title string, content interface{}) {
	var (
		err error
		buf = new(bytes.Buffer)
	)
	if !VerifyEmailFormat(toMail) {
		panic("邮箱格式不对")
	}
	temp, _ := template.New("test_parse").Parse(htmlTemple)
	if content != nil {
		err = temp.Execute(buf, content)
	} else {
		err = temp.Execute(buf, "")
	}
	e := email.NewEmail()
	e.From = "FX Swift <service@ifcgate.com>"
	e.To = []string{toMail}

	e.Subject = title
	e.HTML = []byte(buf.String())
	err = e.Send(
		"smtppro.zoho.eu:587",
		smtp.PlainAuth("", "service@ifcgate.com", "Yun@20231017", "smtppro.zoho.eu"))
	if err != nil {
		panic(err)
	}
}

func VerifyEmailFormat(email string) bool {
	pattern := `\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*` //匹配电子邮箱
	reg := regexp.MustCompile(pattern)
	return reg.MatchString(email)
}
